/*
Rotate Image Total Accepted: 6177 Total Submissions: 20304 My Submissions
You are given an n x n 2D matrix representing an image.

Rotate the image by 90 degrees (clockwise).

Follow up:
Could you do this in-place?
*/

class Solution {
public:
    void rotate(vector<vector<int> > &matrix) {
        int n = matrix.size();
        if (n==0 || n==1) return;
        for (int i = 0; i < n/2; i++) {  // first loop: 0-n/2-1
            for (int j = i; j < n-i-1; j++) {
                // swap the elements on the four quandrant
                int tmp = matrix[i][j];
                matrix[i][j] = matrix[n-j-1][i];
                matrix[n-j-1][i] = matrix[n-i-1][n-j-1];
                matrix[n-i-1][n-j-1] = matrix[j][n-i-1];
                matrix[j][n-i-1] = tmp;
            }
        }
    }
};
